/**
 * Simple lexer application, parse input tokens.
 * Variation on code from : "Language Implementation Patterns", Terence Parr
 */
package org.berlin.octane.compiler.lang;

/**
 * Base lexer. 
 */
public abstract class VisitorBaseLexer {

    public static final char EOF = (char) -1;
    public static final int EOF_TYPE = 1;
    
    private String input;
    private int p = 0;
    private char c;

    /**
     * Constructor for Lexer.
     * 
     * @param input
     *            String
     */
    public VisitorBaseLexer(final String input) {
        this.input = input;
        c = input.charAt(p);
    }

    /**
     * Consume character.
     */
    public void consume() {
        p++;
        if (p >= input.length()) {
            c = EOF;
        } else {
            c = input.charAt(p);
            /// Comment, print each char: System.out.println("[" + p + "::" + c + "]");
        } // End of the if - else //
    }
    
    public char c() {
        return c;
    }
    
    public void setc(final char c) {
        this.c = c;
    }
    
    /**
     * Ensure x is next character on the input stream
     * 
     * @param x
     *            char
     */
    public void match(final char x) {
        if (c == x) {
            consume();
        } else {
            throw new Error("Expecting " + x + "; found " + c);
        }
    }

    /**
     * Scan and consume next token.
     * 
     * @return Token
     */
    public abstract VisitorToken nextToken();

    /**
     * Return token name.
     * 
     * @param tokenType
     *            int
     * @return String
     */
    public abstract String getTokenName(int tokenType);

} // End of the class //
